package com.bawei.user

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModelStores
import androidx.lifecycle.viewModelScope
import com.bawei.base.bean.LoginCodeResult
import com.bawei.base.bean.Res
import com.bawei.base.bean.SendCodeResult
import com.bawei.base.bean.UpdateResult
import com.bawei.base.bean.UserInfoResult
import com.bawei.base.http.SingleLiveData
import com.bawei.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch
import okhttp3.MultipartBody

class UserViewModel:BaseViewModel() {
    //发送验证码
    val sendCodeSuccess = SingleLiveData<Res<SendCodeResult>>()
    val sendCodeFail = SingleLiveData<String>()

    //发送验证码
    fun sendCode(phone:String){

        viewModelScope.launch {
            try {
                sendCodeSuccess.postValue(apiService.sendCode(phone))
            } catch (e:Exception){
                sendCodeFail.postValue(e.message.toString())
            }
        }
    }

    //验证码登录
    val loginCodeSuccess = SingleLiveData<Res<LoginCodeResult>>()
    val loginCodeFail = SingleLiveData<String>()
    //
    fun loginCode(captcha:String,phone:String){
        viewModelScope.launch {
          try {
              var map = mutableMapOf<String,Any>()
              map["captcha"] = captcha
              map["phone"] = phone
              loginCodeSuccess.postValue(apiService.loginCode(maoToBody(map)))
          } catch (e:Exception){
              loginCodeFail.postValue(e.message.toString())
          }
        }
    }

    //用户信息
    val userInFoSuccess = SingleLiveData<Res<UserInfoResult>>()
    val userInFoFail = SingleLiveData<String>()
    //
    fun userInFo(){
        viewModelScope.launch {
            try {
                userInFoSuccess.postValue(apiService.userInFo())
            } catch (e:Exception){
                userInFoFail.postValue(e.message.toString())
            }
        }
    }

    //修改密码
    val updatePwdSuccess = SingleLiveData<Res<Boolean>>()
    val updatePwdFail = SingleLiveData<String>()
    //
    fun updatePwd(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["account"] = account
                map["captcha"] = captcha
                map["password"] = password
                //
                updatePwdSuccess.postValue(apiService.updatePwd(maoToBody(map)))
            } catch (e:Exception){
                updatePwdFail.postValue(e.message.toString())
            }
        }
    }

    //
    val loginPwdSuccess = SingleLiveData<Res<LoginCodeResult>>()
    val loginPwdFail = SingleLiveData<String>()
    //
    fun loginPwd(account:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["account"] = account
                map["password"] = password
                loginPwdSuccess.postValue(apiService.loginPwd(maoToBody(map)))
            } catch (e:Exception){
                loginPwdFail.postValue(e.message.toString())
            }
        }
    }

    //
    val updateSuccess = SingleLiveData<Res<UpdateResult>>()
    val updateFail = SingleLiveData<String>()
    //
    fun update(model:String,part:MultipartBody.Part,pia:String){
        viewModelScope.launch {
            try {
                updateSuccess.postValue(apiService.update(model,part,pia))
            } catch (e: Exception) {
                updateFail.postValue(e.message.toString())
            }
        }
    }

    //
    val editSuccess = SingleLiveData<Res<String?>>()
    val editFail = SingleLiveData<String>()
    //
    fun edit(avatar:String,nickname:String){
        viewModelScope.launch {
            try {
                editSuccess.postValue(apiService.edit(avatar, nickname))
            } catch (e: Exception) {
                editFail.postValue(e.message.toString())
            }
        }
    }
}